package com.bkgy.controller.repOutput.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.controller.acidefficiency.entity.TcmOutputRecord;
import com.bkgy.controller.repOutput.entity.RepOutput;
import com.bkgy.controller.repOutput.mapper.IRepOutputMapper;
import com.bkgy.controller.repOutput.service.IRepOutputService;
import com.bkgy.controller.tpdol3data.entity.TPdoDataL3Cal1630;
import com.bkgy.controller.tpdol3data.entity.TPdoDataL3Cal2150;
import com.bkgy.controller.tpdol3data.entity.TPdoDataL3Cgl1870;
import com.bkgy.controller.tpdol3data.entity.TPdoDataL3Tcm2280;
import com.bkgy.controller.tpdol3data.entity.dto.TpdoL3OutputRes;
import com.bkgy.jobs.outputL3Listener.ICal1630L3Listener;
import com.bkgy.jobs.outputL3Listener.ICal2150L3Listener;
import com.bkgy.jobs.outputL3Listener.ICgl1870L3Listener;
import com.bkgy.jobs.outputL3Listener.ITcm2280L3Listener;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class RepOutputServiceImpl extends ServiceImpl<IRepOutputMapper, RepOutput>
        implements IRepOutputService, ITcm2280L3Listener, ICal2150L3Listener, ICal1630L3Listener, ICgl1870L3Listener {
    private final IRepOutputMapper mapper;

    @Override
    public void initRepOutput0() {
        List<RepOutput> resList = new ArrayList<>();

        String yearStart = DateUtil.getDateToString("yyyy0101");
        String todayZero = DateUtil.getDateToString("yyyyMMdd");
        String monthStart = DateUtil.getDateToString("yyyyMM01");

        //构建酸轧产量产量记录
        RepOutput tcm = new RepOutput();
        tcm.setLineName("tcm2280");
        tcm.setNianchan(mapper.selectTcmOutput(yearStart, todayZero));
        tcm.setYuechan(mapper.selectTcmOutput(monthStart, todayZero));
        resList.add(tcm);

        RepOutput cal2150 = new RepOutput();
        cal2150.setLineName("cal2150");
        cal2150.setNianchan(mapper.selectCal2150Output(yearStart, todayZero));
        cal2150.setYuechan(mapper.selectCal2150Output(monthStart, todayZero));
        resList.add(cal2150);

        RepOutput cal1630 = new RepOutput();
        cal1630.setLineName("cal1630");
        cal1630.setNianchan(mapper.selectCal1630Output(yearStart, todayZero));
        cal1630.setYuechan(mapper.selectCal1630Output(monthStart, todayZero));
        resList.add(cal1630);

        RepOutput cgl = new RepOutput();
        cgl.setLineName("cgl1870");
        cgl.setNianchan(mapper.selectCgl1870Output(yearStart, todayZero));
        cgl.setYuechan(mapper.selectCgl1870Output(monthStart, todayZero));
        resList.add(cgl);

        //TODO 成品发车数据来源不明
        RepOutput chengpin = new RepOutput();
        chengpin.setLineName("prod");
        chengpin.setYuechan(BigDecimal.ZERO);
        chengpin.setNianchan(BigDecimal.ZERO);
        resList.add(chengpin);
        saveBatch(resList);
    }

    @Override
    public void initRepOutput1() {
        Date d = DateUtil.getCurrentCrewStartTime();
        String crewStart = DateUtil.getDateToString(d, "yyyyMMddHHmmss");
        String todayZero = DateUtil.getDateToString("yyyyMMdd");
        String endDate = DateUtil.getDateToString(DateUtil.getNextDayZero(new Date()), "yyyyMMdd");

        //更新班产、日产
        RepOutput rep = new RepOutput();
        rep.setLineName("tcm2280");
        rep.setBanchan(mapper.selectTcmOutput(crewStart, endDate));
        rep.setRichan(mapper.selectTcmOutput(todayZero, endDate));
        rep.setActualRoll(mapper.selectTotalTcmRoll(todayZero));
        updateById(rep);

        RepOutput rep2 = new RepOutput();
        rep2.setLineName("cal2150");
        rep2.setBanchan(mapper.selectCal2150Output(crewStart, endDate));
        rep2.setRichan(mapper.selectCal2150Output(todayZero, endDate));
        updateById(rep2);

        RepOutput rep3 = new RepOutput();
        rep3.setLineName("cal1630");
        rep3.setBanchan(mapper.selectCal1630Output(crewStart, endDate));
        rep3.setRichan(mapper.selectCal1630Output(todayZero, endDate));
        updateById(rep3);

        RepOutput rep4 = new RepOutput();
        rep4.setLineName("cgl1870");
        rep4.setBanchan(mapper.selectCgl1870Output(crewStart, endDate));
        rep4.setRichan(mapper.selectCgl1870Output(todayZero, endDate));
        updateById(rep4);

        RepOutput rep5 = new RepOutput();
        rep5.setLineName("prod");
        rep5.setBanchan(BigDecimal.ZERO);
        rep5.setRichan(BigDecimal.ZERO);
        updateById(rep5);
    }

    @Override
    public ResultData<List<TpdoL3OutputRes>> findOutputData() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("tcm2280", "酸轧2280");
        map.put("cal2150", "连退2150");
        map.put("cal1630", "连退1630");
        map.put("cgl1870", "镀锌1870");
        map.put("prod", "成品发车");

        List<TpdoL3OutputRes> resList = new ArrayList<>();
        List<RepOutput> list = list();
        for (RepOutput obj : list) {
            TpdoL3OutputRes res = new TpdoL3OutputRes();
            res.setGroupName(map.get(obj.getLineName()));
            res.setYearTotal(obj.getNianchan().add(obj.getRichan()));
            res.setMonthTotal(obj.getYuechan().add(obj.getRichan()));
            res.setDayTotal(obj.getRichan());
            res.setCrewTotal(obj.getBanchan());
            resList.add(res);
        }
        return ResultData.ResultDataSuccess(resList, resList.size());
    }

    @Override
    public void updateOutputYearMonthJob(Date d) {
        //昨日0时--今日0时
        String beginDate = DateUtil.getDateToString(d, "yyyyMMdd");
        String endDate = DateUtil.getDateToString(DateUtil.getNextDayZero(d), "yyyyMMdd");
        List<RepOutput> list = list();
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        for (RepOutput obj : list) {
            BigDecimal output = BigDecimal.ZERO;
            String lineName = obj.getLineName();
            if ("tcm2280".equals(lineName)) {
                output = mapper.selectTcmOutput(beginDate, endDate);
            } else if ("cal2150".equals(lineName)) {
                output = mapper.selectCal2150Output(beginDate, endDate);
            } else if ("cal1630".equals(lineName)) {
                output = mapper.selectCal1630Output(beginDate, endDate);
            } else if ("cgl1870".equals(lineName)) {
                output = mapper.selectCgl1870Output(beginDate, endDate);
            }
            //不是1月1日执行
            if (!(month == 1 && day == 1)) {
                obj.setNianchan(obj.getNianchan().add(output));
            }
            //不是1号执行
            if (day != 1) {
                obj.setYuechan(obj.getYuechan().add(output));
            }
            updateById(obj);
        }
    }

    @Override
    public ResultData<TcmOutputRecord> tcmOutputRecord() {
        LambdaQueryWrapper<RepOutput> w = new LambdaQueryWrapper<>();
        w.eq(RepOutput::getLineName, "tcm2280");
        RepOutput obj = getOne(w);
        TcmOutputRecord record = new TcmOutputRecord();
        record.setRecordDay(obj.getRecordDay());
        record.setRecordMonth(obj.getRecordMonth());
        record.setOutputWtDay(obj.getRichan());
        record.setOutputWtDayRecord(obj.getOutputWtDayRecord());
        record.setOutputWtMonth(obj.getYuechan().add(obj.getRichan()));
        record.setOutputWtMonthRecord(obj.getOutputWtMonthRecord());
        record.setOutputWtMonthSet(obj.getOutputWtMonthSet());

        record.setOverRate(record.getOutputWtMonth().divide(record.getOutputWtMonthSet(), 4, BigDecimal.ROUND_HALF_UP));
        return ResultData.ResultDataSuccess(record);
    }

    @Override
    public void updateRecordPerDay() {
        //每日零时执行
        LambdaQueryWrapper<RepOutput> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RepOutput::getLineName, "tcm2280");
        RepOutput record = getOne(queryWrapper);
        //日产量归零
        record.setRichan(BigDecimal.ZERO);
        //1日将月产量归零
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int day = cal.get(Calendar.DAY_OF_MONTH);
        if (day == 1) {
            record.setYuechan(BigDecimal.ZERO);
        }

        updateById(record);
    }

    @Override
    public void hasNew2280Record(List<TPdoDataL3Tcm2280> list) {
        log.info("酸轧检测到新卷");
        for (TPdoDataL3Tcm2280 obj : list) {
            log.info(obj.getIndocno() + ":" + obj.getExtid());
        }

        String crewStart = DateUtil.getDateToString(DateUtil.getCurrentCrewStartTime(), "yyyyMMddHHmmss");
        String todayZero = DateUtil.getDateToString("yyyyMMdd");
        String endDate = DateUtil.getDateToString(DateUtil.getNextDayZero(new Date()), "yyyyMMdd");

        LambdaQueryWrapper<RepOutput> w = new LambdaQueryWrapper<>();
        w.eq(RepOutput::getLineName, "tcm2280");
        RepOutput rep = getOne(w);
        rep.setRichan(mapper.selectTcmOutput(todayZero, endDate));
        rep.setBanchan(mapper.selectTcmOutput(crewStart, endDate));
        rep.setActualRoll(mapper.selectTotalTcmRoll(todayZero));
        updateById(rep);
    }

    @Override
    public void hasNew2150Record(List<TPdoDataL3Cal2150> list) {
        BigDecimal total = BigDecimal.ZERO;
        for (TPdoDataL3Cal2150 obj : list) {
            total = total.add(obj.getOutMatWt());
        }
        LambdaQueryWrapper<RepOutput> w = new LambdaQueryWrapper<>();
        w.eq(RepOutput::getLineName, "cal2150");
        RepOutput rep = getOne(w);
        rep.setBanchan(rep.getBanchan().add(total));
        rep.setRichan(rep.getRichan().add(total));
        updateById(rep);
    }

    @Override
    public void hasNew1630Record(List<TPdoDataL3Cal1630> list) {
        BigDecimal total = BigDecimal.ZERO;
        for (TPdoDataL3Cal1630 obj : list) {
            total = total.add(obj.getOutMatWt());
        }
        LambdaQueryWrapper<RepOutput> w = new LambdaQueryWrapper<>();
        w.eq(RepOutput::getLineName, "cal1630");
        RepOutput rep = getOne(w);
        rep.setBanchan(rep.getBanchan().add(total));
        rep.setRichan(rep.getRichan().add(total));
        updateById(rep);
    }

    @Override
    public void hasNew1870Record(List<TPdoDataL3Cgl1870> list) {
        BigDecimal total = BigDecimal.ZERO;
        for (TPdoDataL3Cgl1870 obj : list) {
            total = total.add(obj.getOutMatWt());
        }
        LambdaQueryWrapper<RepOutput> w = new LambdaQueryWrapper<>();
        w.eq(RepOutput::getLineName, "cgl1870");
        RepOutput rep = getOne(w);
        rep.setBanchan(rep.getBanchan().add(total));
        rep.setRichan(rep.getRichan().add(total));
        updateById(rep);
    }
}
